package cn.iocoder.yudao.module.game.controller.admin.agentSummary;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.agentSummary.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.agentSummary.AgentSummaryDO;
import cn.iocoder.yudao.module.game.service.agentSummary.AgentSummaryService;

@Tag(name = "管理后台 - 代理汇总")
@RestController
@RequestMapping("/game/agent-summary")
@Validated
public class AgentSummaryController {

    @Resource
    private AgentSummaryService agentSummaryService;

    @PostMapping("/create")
    @Operation(summary = "创建代理汇总")
    @PreAuthorize("@ss.hasPermission('game:agent-summary:create')")
    public CommonResult<Long> createAgentSummary(@Valid @RequestBody AgentSummarySaveReqVO createReqVO) {
        return success(agentSummaryService.createAgentSummary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理汇总")
    @PreAuthorize("@ss.hasPermission('game:agent-summary:update')")
    public CommonResult<Boolean> updateAgentSummary(@Valid @RequestBody AgentSummarySaveReqVO updateReqVO) {
        agentSummaryService.updateAgentSummary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理汇总")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:agent-summary:delete')")
    public CommonResult<Boolean> deleteAgentSummary(@RequestParam("id") Long id) {
        agentSummaryService.deleteAgentSummary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理汇总")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:agent-summary:query')")
    public CommonResult<AgentSummaryRespVO> getAgentSummary(@RequestParam("id") Long id) {
        AgentSummaryDO agentSummary = agentSummaryService.getAgentSummary(id);
        return success(BeanUtils.toBean(agentSummary, AgentSummaryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理汇总分页")
    @PreAuthorize("@ss.hasPermission('game:agent-summary:query')")
    public CommonResult<PageResult<AgentSummaryRespVO>> getAgentSummaryPage(@Valid AgentSummaryPageReqVO pageReqVO) {
        PageResult<AgentSummaryDO> pageResult = agentSummaryService.getAgentSummaryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AgentSummaryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理汇总 Excel")
    @PreAuthorize("@ss.hasPermission('game:agent-summary:export')")
    @OperateLog(type = EXPORT)
    public void exportAgentSummaryExcel(@Valid AgentSummaryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AgentSummaryDO> list = agentSummaryService.getAgentSummaryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "代理汇总.xls", "数据", AgentSummaryRespVO.class,
                        BeanUtils.toBean(list, AgentSummaryRespVO.class));
    }

}